<?php

namespace App\Repositories;

use App\Models\TestingConfig;
use Cache;
class TestingConfigRepository extends Repository
{
    static $tag = 'testing.config';

    public function getValue($key, $default = null, $deleteHtmlTag = false)
    {
        $map = $this->get($key);
        $val = $map ? $map->value : $default;
        $val = $deleteHtmlTag ? strip_tags($val) : $val;
        return $val;
    }


    public function get($key)
    {
        $cacheKey = getCacheKey('testing.cache_key.system.key') . $key;
        $value = $this->remember($cacheKey, function () use ($key) {
            return TestingConfig::where('key', $key)->first();
        });
        return $value;
    }


    public function getAll()
    {
        $cacheKey = getCacheKey('testing.cache_key.system.list');
        $maps = $this->remember($cacheKey, function () {
            return TestingConfig::all();
        });
        return $maps;
    }

    public function getByTag($tag)
    {
        $cacheKey = getCacheKey('testing.cache_key.system.tag') . $tag;
        $maps = $this->remember($cacheKey, function () use ($tag) {
            return TestingConfig::where('tag', $tag)->get();
        });
        return $maps;
    }

    public function count($tag = null)
    {
        $cacheKey = getCacheKey('testing.cache_key.system.tag_count') . $tag;
        $count = $this->remember($cacheKey, function () use ($tag) {
            if ($tag) {
                return TestingConfig::where('tag', $tag)->count();
            }
            return TestingConfig::count();
        });
        return $count;
    }

    public function saveSettings(array $inputs)
    {
        foreach ($inputs as $key => $value) {
            $map = TestingConfig::firstOrNew([
                'key' => $key,
            ]);
            $map->tag = 'settings';
            $map->value = $value;
            $map->save();
        }
        $this->clearCache();
    }

    /**
     * 构建选项
     */
    public function buildSelectOptions($key){
        $questionTypeVal = $this->getValue($key);
        $types = [];
        if($questionTypeVal){
            $typesArr = explode(',',$questionTypeVal);
            foreach ($typesArr as $val){
                $keyVals = explode(':',$val);
                $obj['value'] = $keyVals[0];
                $obj['text'] = $keyVals[1];
                array_push($types,$obj);
            }
        }
        return $types;
    }


    /**
     * 转换选项中具体的值
    */
    public function convertOptions($key,$value){
        $str = '';
        $questionTypes = $this->buildSelectOptions($key);
        if(count($questionTypes)>0){
            foreach ($questionTypes as $type){
                if($value == $type['value']){
                    $str = $type['text'];
                    break;

        }
            }
        }
        return $str;
    }

    public function getThirdPartyUnionCodes(){
        $unionCodeStr = $this->getValue('thirdparty.join.testing.union.code',null,true);
        $unionCodes = explode(',',$unionCodeStr);
        return $unionCodes;
    }
    
    /**
     * 减重测评题目的获取tag对应的值
     * @param $index 题目编号
     */
    public function getDWTestingValuesByTag($index){
        $tag = 'DW.resist.item.'.$index;
        return $this->getByTag($tag);
    }

    public function getDWTestingFatWaistResult(){
        $tag = 'DW.fat.waist.result';
        return $this->getByTag($tag);
    }


    public function cacheTime()
    {
        return config('testing.common_cache_time');
    }


    public function model()
    {
        return app(TestingConfig::class);
    }

    public function tag()
    {
        return self::$tag;
    }
}